#!/bin/sh
# Copyright (c) Huawei Technologies Co., Ltd. 2012-2023. All rights reserved.
# Arguments: SCRIPT_ROOT_PATH=$1 PID=$2
# Returns: 0 is success, other is fail, result_file return asm info
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

set +x

SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3

. "${SCRIPT_ROOT_PATH}"/oracle_sbin_func.sh

PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`

ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORA_GRID_USER=`get_value "${PARAM_CONTENT}" GridInstallUser`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`

#get oracle user info
get_oracle_user
if [ -z "${ORA_DB_USER}" ] || [ -z "${ORA_DB_GROUP}" ]; then
    log_error "Get oracle user failed"
    exit "${RETURN_INVALID}"
fi
get_oracle_user_shell_type

env_file_list=`get_user_env_file "${ORA_DB_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
if [ $? -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

# check oracle install
check_oracle_is_install

# check oracle install type
check_oracle_install_type
if [ "${DBISCLUSTER}" -eq 0 ]; then
    log_warn "Oracle is not a rac or asm node, can not get asm info"
    exit 0
fi

#get oracle version
get_oracle_version

# get grid user
get_grid_user
# get grid user shell type
get_grid_user_shell_type
# get grid home path
get_grid_home_path
if [ -z "${ORA_GRID_USER}" ] || [ -z "${ORA_GRID_GROUP}" ] ; then
    log_error "Get grid user failed"
    exit "${RETURN_INVALID}"
fi

env_file_list=`get_user_env_file "${ORA_GRID_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_GRID_ENV=`generate_export_cmd "$env_file_list" "grid"`
if [ $? -ne 0 ] || [ -z "$EXPORT_GRID_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi



log_info "Begin to read oracle asm information."
delete_file "${RESULT_FILE}"

# check ASM instance
asm_inst=`ps -ef | grep asm_...._+ | grep -v grep | ${MYAWK} -F_ '{print $NF}' | uniq`
if [ -z "$asm_inst" ]; then
    log_error "Have no asm instance"
    exit "${RETURN_INVALID}"
fi

get_asm_auth "${asm_inst}"
auth_type=$?
if [ "${auth_type}" -eq "2" ]; then
    log_error "Get asm auth type is failed"
    exit "${RETURN_INVALID}"
fi
echo "${asm_inst};${auth_type};${DBISCLUSTER}" >> "${RESULT_FILE}"
log_info "Asm status:${asm_inst}, auty type:${auth_type}, db type:${DBISCLUSTER}"
#chmod 640 RESULT_FILE
if [ -f "${RESULT_FILE}" ]; then
    chmod 640 "${RESULT_FILE}"
fi
log_info "Get oracle asm succ."
exit 0